<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="文件组名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主机地址" prop="hostip" label-width="75">
        <el-input
          v-model="queryParams.hostip"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属业务系统" prop="busisystemid" label-width="100">
        <el-select
          v-model="queryParams.busisystemid"
          placeholder="请选择系统"
        >
          <el-option
            v-for="item in systemList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            @keyup.enter="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文件模式：export/import" prop="inorex" label-width="190">
        <el-select
          v-model="queryParams.inorex"
          placeholder="请输入字典，对于业务系统而言，这个文件组是输入inport还是输出export。"
        >
          <el-option
            v-for="item in fileModeList"
            :key="item.mode"
            :label="item.mode"
            :value="item.mode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="监控模式" prop="testmode">
        <el-select
          v-model="queryParams.testmode"
          placeholder="若不填，则使用子模式的监控模式"
        >
          <el-option
            v-for="item in modeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['operation:fileGroup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operation:fileGroup:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operation:fileGroup:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['operation:fileGroup:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="link"
          @click="jumpFileResult"
          v-hasPermi="['operation:fileGroup:list']"
        >查看检测结果</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileGroupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件组名" align="center" prop="name" />
      <el-table-column label="主机地址" align="center" prop="hostip" />
      <el-table-column label="主要路径" align="center" prop="fullpath" />
      <el-table-column label="业务系统" align="center" prop="busisystemid">
        <template #default="scope">
          {{getbusiName(scope.row.busisystemid)}}
        </template>
      </el-table-column>
      <el-table-column label="文件模式" align="center" prop="inorex" />
      <el-table-column label="监控模式" align="center" prop="testmode">
        <template #default="scope">
<!--          <dict-tag :options="modeList" :value="scope.row.testmode"/>-->
          <el-tag type="success" v-if="scope.row.testmode==2">根据清单匹配不为空</el-tag>
          <el-tag type="success" v-if="scope.row.testmode==1">对比昨天工作日</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="监控时间" align="center">
        <template #default="scope">
<!--          {{parseTime(scope.row.starttime,"{h}:{i}:{s}")}}-->
          {{scope.row.starttime}}
        </template>
      </el-table-column>
      <el-table-column label="period" align="center" prop="period" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="450">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['operation:fileGroup:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['operation:fileGroup:remove']">删除</el-button>
              <el-button link type="primary" icon="SwitchButton" @click="handleExecute(scope.row)" v-hasPermi="['operation:fileGroup:edit']">执行</el-button>
              <el-button
                link
                type="primary"
                icon="Setting"
                @click="handleAddGroupList(scope.row.id)"
                v-hasPermi="['operation:fileGroup:remove']"
              >配置子项</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件组对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="fileGroupRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="组名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="主机ip" prop="hostip">
          <el-input v-model="form.hostip" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="主要路径" prop="fullpath">
          <el-input v-model="form.fullpath" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="业务系统" prop="busisystemid">
          <el-select
            v-model="form.busisystemid"
            placeholder="请选择系统"
          >
            <el-option
              v-for="item in systemList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              @keyup.enter="handleQuery"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="文件模式" prop="inorex">
          <el-select
            v-model="form.inorex"
            placeholder="请输入字典，对于业务系统而言，这个文件组是输入inport还是输出export。"
          >
            <el-option
              v-for="item in fileModeList"
              :key="item.mode"
              :label="item.mode"
              :value="item.mode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="对比模式" prop="testmode">
          <el-select
            v-model="form.testmode"
          >
            <el-option
              v-for="item in modeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" type="textarea" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否启用" prop="memo">
          <el-select
            v-model="form.flag"
            placeholder="是否启动自动监控"
          >
            <el-option
              v-for="item in flagList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="period" prod="period">
          <el-input v-model="form.period" placeholder="周期"/>
        </el-form-item>
        <el-form-item label="检测时间" prod="starttime">
          <el-time-select
            v-model="form.starttime"
            type="date"
            start="00:00"
            step="00:30"
            end="23:59"
            format="HH:mm:ss"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FileGroup">
import { listFileGroup, getFileGroup, delFileGroup, addFileGroup, updateFileGroup ,executeMonitor} from "@/api/operation/fileGroup";
import {getCurrentInstance} from "vue";
import {getSysList} from "@/api/operation/sqltool";
import {parseTime} from "@/utils/ruoyi";
import DictTag from "@/components/DictTag/index.vue";

const { proxy } = getCurrentInstance();
const fileGroupList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const systemList = ref();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    hostip: undefined,
    fullpath: undefined,
    busisystemid: undefined,
    inorex: undefined,
    period: undefined,
    starttime: undefined,
    testmode: undefined,
    memo: undefined,
    flag: undefined,
  },
  rules: {
    id: [
      { required: true, message: "主键不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    hostip: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    fullpath: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    busisystemid: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    inorex: [
      { required: true, message: "字典，对于业务系统而言，这个文件组是输入inport还是输出export。不能为空", trigger: "blur" }
    ],
    period: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    starttime: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    // testmode: [
    //   { required: true, message: "1：根据上工作日对比；2：根据清单表匹配不能为空", trigger: "blur" }
    // ],
    memo: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    flag: [
      { required: true, message: "0：不启用；1:启用；不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);
const modeList =[
  {
    label:"根据清单匹配",
    value:"2"
  },
  {
    label:"对比上工作日",
    value:"1"
  }
]
const fileModeList = [
  {
    mode:"export"
  },
  {
    mode:"import"
  }
]
const flagList = [
  {
    label:"开启",
    value:"1"
  },
  {
    label:"不开启",
    value:"0"
  }
]
/** 查询文件组列表 */
function getList() {
  loading.value = true;
  listFileGroup(queryParams.value).then(response => {
    fileGroupList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getBusiList(){
  getSysList().then(response => {
    systemList.value = response.rows;
    console.log(systemList,"====");
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
function getbusiName(busisystemid) {
  return systemList.value.find(item => item.id === busisystemid).name;
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    hostip: null,
    fullpath: null,
    busisystemid: null,
    inorex: null,
    period: null,
    starttime: null,
    testmode: null,
    memo: null,
    flag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("fileGroupRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加文件组";
}
function handleExecute(data) {
  console.log(data.id)
  executeMonitor(data.id).then(response => {
    proxy.$modal.msgSuccess("执行成功");
    getList();
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getFileGroup(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改文件组";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["fileGroupRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateFileGroup(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addFileGroup(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除文件组编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delFileGroup(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('/operation/fileGroup/export', {
    ...queryParams.value
  }, `fileGroup_${new Date().getTime()}.xlsx`)
}

function jumpFileResult(){
  proxy.$router.push("/operation/file-group-list/result")
}

function handleAddGroupList(row){
  console.log(row)
  proxy.$router.push("/operation/file-group-list/index/"+row)
}
getBusiList()
getList();
</script>
